package com.atguigu.edu.realtime.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {
    private static JedisPool jedisPool;
    //饿汉式 类加载创建连接池对象  类只会被加载一次，池也会被创建一次
    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100);//池最大连接数
        poolConfig.setMaxIdle(5);//最大空闲连接
        poolConfig.setMinIdle(5);//初始化连接
        poolConfig.setBlockWhenExhausted(true);//资源耗尽，等待2S，这里已经连上了服务器
        poolConfig.setMaxWaitMillis(2000);
        poolConfig.setTestOnBorrow(true);//发送一个ping,返回一个pong
        poolConfig.setTestOnCreate(false);
        //连接服务器允许超时时间，还没连上服务器
        jedisPool = new JedisPool(poolConfig,"hadoop101",6379,10000);
    }

    public static Jedis getJedis(){
        System.out.println("~~~获取Jedis客户端~~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String pong = jedis.ping();
        System.out.println(pong);
    }
}

